package com.gimi.cloud.jimi.manage.service.userInfo.Impl;

import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dao.dao.userInfo.RdJxlDao;
import com.gimi.cloud.jimi.dao.dao.userInfo.RdMiguanLogDao;
import com.gimi.cloud.jimi.dto.userInfo.RdJxlDTO;
import com.gimi.cloud.jimi.dto.userInfo.RdMiguanDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdJxl;
import com.gimi.cloud.jimi.entity.userInfo.RdMiguan;
import com.gimi.cloud.jimi.manage.service.userInfo.JxlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JxlServiceImpl implements JxlService {
    @Autowired
    private RdJxlDao rdJxlDao;
    @Autowired
    private RdMiguanLogDao rdMiguanLogDao;

    @Override
    public RdJxlDTO getUserJxl(RdJxlDTO param) {
        RdJxl jxl = rdJxlDao.getNewestJxlByUser(param.getUserId());
        RdJxlDTO jxlDTO = new RdJxlDTO();
        BeanUtils.copyProperties(jxl, jxlDTO);
        if(!StringUtil.isBlank(jxlDTO.getReportData()) && !jxlDTO.getGetdata().startsWith("{")) {
            jxlDTO.setReportData(AliOSSUtil.getFileUrl(jxlDTO.getReportData(), AliOSSUtil.getNoEffictDateByDay(1)));
        }
        return jxlDTO;
    }

    @Override
    public void saveUserJxlReport(RdJxlDTO jxlDTO) {
        RdJxl jxl = new RdJxl();
        BeanUtils.copyProperties(jxlDTO, jxl);
        rdJxlDao.updateByPrimaryKey(jxl);
    }

    @Override
    public RdMiguanDTO getUserMiguan(RdMiguanDTO param) {
        RdMiguan miguan = rdMiguanLogDao.findIsUpload(param.getUserId());
        if(miguan != null) {
            RdMiguanDTO miguanDTO = new RdMiguanDTO();
            BeanUtils.copyProperties(miguan, miguanDTO);
            if(!StringUtil.isBlank(miguan.getOssUrl())) {
                miguanDTO.setOssUrl(AliOSSUtil.getFileUrl(miguanDTO.getOssUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
            }
        }
        return null;
    }
}
